package com.example.panda;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Login extends AppCompatActivity {
    private EditText mUsername;
    private EditText mPassWord;
    private Button bt;
    private CheckBox checkUser;
    private CheckBox checkPass;
    String username;
    String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

         bt = (Button)findViewById(R.id.login_bt);
         mUsername = (EditText)findViewById(R.id.login_etUser);
         mPassWord = (EditText)findViewById(R.id.login_etPass);
         checkUser = (CheckBox)findViewById(R.id.login_cbuser);
         checkPass = (CheckBox)findViewById(R.id.login_cbpass);
         readfrominfo();
         readfrominfpass();

         bt.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 if (checkUser.isChecked() ){
                     username = mUsername.getText().toString();
                     File file = new File(getFilesDir(),"info.txt");
                     FileOutputStream fos = null;
                     try {
                         fos = new FileOutputStream(file);
                     } catch (FileNotFoundException e) {
                         e.printStackTrace();
                     }try {
                         fos.write(username.getBytes());
                         fos.close();
                     } catch (IOException e) {
                         e.printStackTrace();

                     }

                 }
                 if (checkPass.isChecked()){
                    password = mPassWord.getText().toString();
                     File file = new File(getFilesDir(),"infopass.txt");
                     FileOutputStream fos = null;
                     try {
                         fos = new FileOutputStream(file);
                     } catch (FileNotFoundException e) {
                         e.printStackTrace();
                     }try {
                         fos.write(password.getBytes());
                         fos.close();
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                 }

                 Toast.makeText(Login.this,"登录成功",Toast.LENGTH_SHORT).show();
                 Intent intent = new Intent(Login.this,MainActivity.class);
                 startActivity(intent);
             }
         });


    }

    public void readfrominfo(){
        File file = new File(getFilesDir(),"info.txt");
        if (file.exists()){
            BufferedReader br = null;
            try {
//                字节流转化为输入流然后转化为字符流
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String str = null;
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

//            String []  up = str.split("##");    //按什么分割
            mUsername.setText(str);
        }
    }
    public void readfrominfpass(){
        File file = new File(getFilesDir(),"infopass.txt");
        if (file.exists()){
            BufferedReader br = null;
            try {
//                字节流转化为输入流然后转化为字符流
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String str = null;
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }

//            String []  up = str.split("##");    //按什么分割
            mPassWord.setText(str);
        }
    }
}
